package com.example.netty.Demo0;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class UnsafeUser2 {
    public static void main(String[] args) throws Exception {
        //通过反射实例化Unsafe
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe) f.get(null);
        Field f1 = User2.class.getDeclaredField("name");
        Field f2 = User2.class.getDeclaredField("age");
        //获取普通变量的偏移量
        System.out.println(unsafe.objectFieldOffset(f1));
        //获取静态变量的偏移量
        System.out.println(unsafe.staticFieldOffset(f2));
    }

}
class User2 {

    private static Integer age = 22;
    private String name;

    private User2() {
        System.out.println("Constracter userd");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static Integer getAge() {
        return age;
    }
}
